<template>
  <div class="app-box">
    <!-- <button @click="testReadCard">测试读卡</button> -->
    <Layout ref="runBox"
            title="GateOutManual 终端"
            temp="Temp2"
            @handleSuccessMqttMessage="handleSuccessMqttMessage"
            @showCard89Info="showCard89Info"
            :ic="ic"
            :url="url"
            :readID="false"
            :readIC="true"></Layout>
  </div>
</template> 
<script>
import {
  log,
} from '@/utils/message'
import Layout from '../core/layout.vue'
import { GSAConstants } from '@/utils/access'
import { ipcRenderer } from 'electron'
export default {
  components: { Layout },
  data () {
    return {
      ic: GSAConstants().TOPIC_PREADVICE_MANUAL_OUT.ic,
      url: GSAConstants().TOPIC_PREADVICE_MANUAL_OUT.url
    }
  },
  created () {
  },
  mounted () {
    ipcRenderer.on('write-card-reply', (event, res) => {
      log('write-card-reply', res)
      if (res.readCode === 1) {
        this.showMsg('放行信息写入成功， 欢迎下次再来！', 'green')
      } else {
        this.showMsg(this.parseCardCode(res.readCode), 'error')
      }
    })

    ipcRenderer.on('read-card-reply', (event, res) => {
      log('read-card-reply', res)
      if (res.readCode === 1) {
        log('TestReadCardSuccess', JSON.stringify(res))

        this.$refs.runBox.doPageFun('showCardInfo89Dialog', res)

      } else {
        this.showMsg(this.parseCardCode(res.readCode), 'error')
      }
    })
  },
  methods: {
    showMsg (msg, type) {
      this.$refs.runBox.showMsg(msg, type)
    },
    parseCardCode (code) {
      const head = '注意！操作卡失败，请检查您的卡或联系服务台'

      if (code === -222) {
        return head + ' 密码装载错误！ '
      }
      if (code === -401) {
        return head + ' 卡操作频繁或认证有误, 无效卡！ '
      }

      if (code === -998) {
        return head + ' 写卡异常， 请检查卡类型! '
      }

      if (code === 2) {
        return head + ' 请重新放卡! '
      }

      if (code === 3) {
        return head + ' 防冲突出错! '
      }

      return head
    },
    showCard89Info () {
      ipcRenderer.send('read-card', { mode: 4 })

      // this.$refs.runBox.doPageFun('showCardInfo89Dialog', {
      // p8: '1',
      // p9: '2'
      // })

      // this.showCardInfo89Dialog({ p8: '123', p9: '456' })

    },
    testReadCard () {
      // for (let index = 0; index < 3; index++) {
      //   this.sleep(3)
      //   ipcRenderer.send('read-card', { mode: 4 })
      // }
      ipcRenderer.send('read-card', { mode: 4 })
    },
    // 读卡成功的回调信息
    handleSuccessMqttMessage (messageObject) {
      if (messageObject.ic === this.ic) {
        const qd = {
          p8: messageObject.dt.contInfo8,
          p9: messageObject.dt.contInfo9
        }
        ipcRenderer.send('write-card', qd)

        if (messageObject.dt.messageCode === 0) {
          this.showMsg(messageObject.dt.message, 'error')
        } else {
          this.showMsg('刷卡操作成功', 'green')
        }

        // 清除表单
        this.$refs.runBox.handleSuccess()
      }
    }
  },
}
</script>

<style scoped>
.app-box {
  padding: 0px;
}
</style>
